package com.basedefence;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.andengine.engine.Engine;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import com.basedefence.models.AnimatedGameObject;
import com.basedefence.models.DefaultRocket;
import com.basedefence.models.GameObject;

import android.content.res.AssetManager;


public class SpriteLoader
{
	private static BitmapTextureAtlas mBitmapTextureAtlas;
	private static HashMap<String,TiledTextureRegion> mAnimatedSprites;
	private static HashMap<String,ITextureRegion> mTextureRegions;
	
	public static void loadAnimatedGameObject (Engine mEngine, String imageName, int cols, int rows)
	{
		if (mAnimatedSprites == null)
			mAnimatedSprites = new HashMap<String, TiledTextureRegion>();
		
		TiledTextureRegion animation = null ;
		
		ITexture texture;
		
		int frameWidth = -1;
		int frameHeight = -1;
		int xindex = -1;
		int yindex = -1;
		int border = -1;
		int margin = -1;
		
		String spriteName ="gfx/" +  imageName + ".spr";
		
		try {
			AssetManager am = BaseDefenceActivity.getInstance().getAssets();
			InputStream is = am.open(spriteName);
			
			DataInputStream spriteStream = new DataInputStream(is);
			
			frameWidth = spriteStream.readShort();
			frameHeight = spriteStream.readShort();
			xindex = spriteStream.readShort();
			yindex = spriteStream.readShort();
			border = spriteStream.readShort();
			margin = spriteStream.readShort();
		    if(mBitmapTextureAtlas == null)
		    	mBitmapTextureAtlas = new BitmapTextureAtlas(BaseDefenceActivity.getInstance().getTextureManager(),1024, 512);
		    animation = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, BaseDefenceActivity.getInstance(),"gfx/" + imageName, 0, 0, cols,rows);
		    mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);
		    mAnimatedSprites.put(imageName, animation);
//			texture = TextureManager.getTexture(imageName, frameWidth, frameHeight, xindex, yindex, border, margin);
			
			byte animAmount = spriteStream.readByte();
//			for (int i = 0; i < animAmount; i++) 
//			{
//				ArrayList<AnimatedSprite.Frame> frames = new ArrayList<AnimatedSprite.Frame>();
//				short framesAmount = spriteStream.readShort();
//				
//				for (int j = 0; j < framesAmount; j++) 
//				{
//					short frameX = spriteStream.readShort();
//					short frameY = spriteStream.readShort();
//					
//					frames.add(new AnimatedSprite.Frame(frameX, frameY));
//				}
//				AnimatedSprite spr = new AnimatedSprite(texture, 0, 0);
//				spr.animate(100, frames);
//				animations.add(spr);
//			}
			
			mAnimatedSprites.put(imageName, animation);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static AnimatedGameObject getAnimatedGameObject (String imageName,GameObject.OBJECT_TYPE type)
	{
		if (mAnimatedSprites == null || !mAnimatedSprites.containsKey(imageName))
			return null;
		AnimatedGameObject object = null;
		switch (type) {
		case BACKGROUND:
			
			break;
		case BASE:
			break;
		case DEF_ROCKET:
			object = new DefaultRocket((float)0,(float)0,mAnimatedSprites.get(imageName).deepCopy(),BaseDefenceActivity.getInstance().getVertexBufferObjectManager());
			break;
		}
//		return spriteAnimations == null ? null : spriteAnimations.get(animIndex).clone();
		return object;
	}
	
	public static GameObject getGameObject(String name,int w, int h)
	{
		return getGameObject(name,w,h,0,0);
	}
	
	public static GameObject getGameObject (String name, int w, int h, int pX, int pY)
	{
		if(mTextureRegions == null || !mTextureRegions.containsKey(name))
			return null;
		GameObject sprite = new GameObject(pX, pY,w,h, mTextureRegions.get(name), BaseDefenceActivity.getInstance().getVertexBufferObjectManager());
		return sprite;
	}
	
	public static GameObject getGameObject(String name)
	{
		return getGameObject(name,0,0,0,0);
	}
	
	public static void unloadGameObject (String imageName)
	{
		if (mAnimatedSprites == null)
			return;
		mAnimatedSprites.remove(imageName);
		if(mTextureRegions == null)
			return;
		mTextureRegions.remove(imageName);
	}

	public static boolean loadGameObject(Engine mEngine, final String name) {
	    ITexture texture;
	    ITextureRegion region;
	    if(mTextureRegions == null)
	    	mTextureRegions = new HashMap<String, ITextureRegion>();
	    boolean res;
		try {
			texture = new BitmapTexture(BaseDefenceActivity.getInstance().getTextureManager(), new IInputStreamOpener() {
			    @Override
			    public InputStream open() throws IOException {
			        return BaseDefenceActivity.getInstance().getAssets().open("gfx/" + name);
			    }
			});
		    texture.load();
		    region = TextureRegionFactory.extractFromTexture(texture);
		    mTextureRegions.put(name, region);
		    res = true;
		} catch (IOException e) {
			e.printStackTrace();
			res = false;
		}
		return res;
	}
}
